From 91e7b5a93c6bcc9ba30101107cf75c488db9b769 Mon Sep 17 00:00:00 2001 From: Happy-melon Date: Fri, 18 Mar 2011 19:15:56 +0000 Subject: [PATCH] More cleanup in Block.php. Push Block::encodeExpiry() and Block::decodeExpiry() deeper into the callstack, to DatabaseBase for encode and Language for decode. The vast majority of callers of these functions are not handling block expiries, but expiries generally, particularly page protections. --- includes/Article.php | 2 +- includes/Block.php | 21 +++++----- includes/OutputPage.php | 16 +------- includes/Title.php | 42 +++++++++----------- includes/api/ApiQueryBlocks.php | 4 +- includes/api/ApiQueryInfo.php | 9 +++-- includes/api/ApiQueryProtectedTitles.php | 3 +- includes/db/Database.php | 14 +++++++ includes/specials/SpecialProtectedpages.php | 18 ++++++--- includes/specials/SpecialProtectedtitles.php | 2 +- languages/Language.php | 31 +++++++++++++++ 11 files changed, 99 insertions(+), 63 deletions(-) diff --git a/includes/Article.php b/includes/Article.php index 254403c44a..ad75953fd0 100644 --- a/includes/Article.php +++ b/includes/Article.php @@ -2617,7 +2617,7 @@ class Article { if ( !isset( $expiry[$action] ) ) $expiry[$action] = Block::infinity(); - $encodedExpiry[$action] = Block::encodeExpiry( $expiry[$action], $dbw ); + $encodedExpiry[$action] = $dbw->encodeExpiry( $expiry[$action] ); if ( $restrictions != '' ) { $protect_description .= "[$action=$restrictions] ("; if ( $encodedExpiry[$action] != 'infinity' ) { diff --git a/includes/Block.php b/includes/Block.php index 422c12b1e1..8314c6fefc 100644 --- a/includes/Block.php +++ b/includes/Block.php @@ -45,7 +45,7 @@ class Block { $this->mAuto = $auto; $this->mAnonOnly = $anonOnly; $this->mCreateAccount = $createAccount; - $this->mExpiry = self::decodeExpiry( $expiry ); + $this->mExpiry = $expiry; $this->mEnableAutoblock = $enableAutoblock; $this->mHideName = $hideName; $this->mBlockEmail = $blockEmail; @@ -342,7 +342,7 @@ class Block { $this->mAllowUsertalk = $row->ipb_allow_usertalk; $this->mHideName = $row->ipb_deleted; $this->mId = $row->ipb_id; - $this->mExpiry = self::decodeExpiry( $row->ipb_expiry ); + $this->mExpiry = $row->ipb_expiry; if ( isset( $row->user_name ) ) { $this->mByName = $row->user_name; @@ -420,7 +420,7 @@ class Block { 'ipb_anon_only' => $this->mAnonOnly, 'ipb_create_account' => $this->mCreateAccount, 'ipb_enable_autoblock' => $this->mEnableAutoblock, - 'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ), + 'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ), 'ipb_range_start' => $this->mRangeStart, 'ipb_range_end' => $this->mRangeEnd, 'ipb_deleted' => intval( $this->mHideName ), // typecast required for SQLite @@ -460,7 +460,7 @@ class Block { 'ipb_anon_only' => $this->mAnonOnly, 'ipb_create_account' => $this->mCreateAccount, 'ipb_enable_autoblock' => $this->mEnableAutoblock, - 'ipb_expiry' => self::encodeExpiry( $this->mExpiry, $dbw ), + 'ipb_expiry' => $dbw->encodeExpiry( $this->mExpiry ), 'ipb_range_start' => $this->mRangeStart, 'ipb_range_end' => $this->mRangeEnd, 'ipb_deleted' => $this->mHideName, @@ -778,6 +778,7 @@ class Block { * @param $expiry String: timestamp for expiry, or * @param $db Database object * @return String + * @deprecated since 1.18; use $dbw->encodeExpiry() instead */ public static function encodeExpiry( $expiry, $db ) { if ( $expiry == '' || $expiry == Block::infinity() ) { @@ -793,13 +794,11 @@ class Block { * @param $expiry String: Database expiry format * @param $timestampType Requested timestamp format * @return String + * @deprecated since 1.18; use $wgLang->decodeExpiry() instead */ public static function decodeExpiry( $expiry, $timestampType = TS_MW ) { - if ( $expiry == '' || $expiry == Block::infinity() ) { - return Block::infinity(); - } else { - return wfTimestamp( $timestampType, $expiry ); - } + global $wgContLang; + return $wgContLang->formatExpiry( $expiry, $timestampType ); } /** @@ -872,8 +871,10 @@ class Block { * * @param $encoded_expiry String: Database encoded expiry time * @return Html-escaped String + * @deprecated since 1.18; use $wgLang->formatExpiry() instead */ public static function formatExpiry( $encoded_expiry ) { + global $wgContLang; static $msg = null; if ( is_null( $msg ) ) { @@ -885,7 +886,7 @@ class Block { } } - $expiry = self::decodeExpiry( $encoded_expiry ); + $expiry = $wgContLang->formatExpiry( $encoded_expiry, TS_MW ); if ( $expiry == self::infinity() ) { $expirystr = $msg['infiniteblock']; } else { diff --git a/includes/OutputPage.php b/includes/OutputPage.php index b4059fe771..4471d771e4 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -1910,20 +1910,8 @@ class OutputPage { $blockid = $wgUser->mBlock->mId; $blockExpiry = $wgUser->mBlock->mExpiry; - if ( $blockExpiry == 'infinity' ) { - // Entry in database (table ipblocks) is 'infinity' but 'ipboptions' uses 'infinite' or 'indefinite' - // Search for localization in 'ipboptions' - $scBlockExpiryOptions = wfMsg( 'ipboptions' ); - foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) { - if ( strpos( $option, ':' ) === false ) { - continue; - } - list( $show, $value ) = explode( ':', $option ); - if ( $value == 'infinite' || $value == 'indefinite' ) { - $blockExpiry = $show; - break; - } - } + if ( $blockExpiry == Block::infinity() ) { + $blockExpiry = wfMessage( 'infiniteblock' ); } else { $blockExpiry = $wgLang->timeanddate( wfTimestamp( TS_MW, $blockExpiry ), diff --git a/includes/Title.php b/includes/Title.php index 9141474c3e..d878642e8c 100644 --- a/includes/Title.php +++ b/includes/Title.php @@ -1566,20 +1566,7 @@ class Title { $blockExpiry = $user->mBlock->mExpiry; $blockTimestamp = $wgLang->timeanddate( wfTimestamp( TS_MW, $user->mBlock->mTimestamp ), true ); if ( $blockExpiry == 'infinity' ) { - // Entry in database (table ipblocks) is 'infinity' but 'ipboptions' uses 'infinite' or 'indefinite' - $scBlockExpiryOptions = wfMsg( 'ipboptions' ); - - foreach ( explode( ',', $scBlockExpiryOptions ) as $option ) { - if ( !strpos( $option, ':' ) ) - continue; - - list( $show, $value ) = explode( ':', $option ); - - if ( $value == 'infinite' || $value == 'indefinite' ) { - $blockExpiry = $show; - break; - } - } + $blockExpiry = wfMessage( 'infiniteblock' ); } else { $blockExpiry = $wgLang->timeanddate( wfTimestamp( TS_MW, $blockExpiry ), true ); } @@ -1680,10 +1667,10 @@ class Title { $dbw = wfGetDB( DB_MASTER ); - $encodedExpiry = Block::encodeExpiry( $expiry, $dbw ); + $encodedExpiry = $dbw->encodeExpiry( $expiry ); $expiry_description = ''; - if ( $encodedExpiry != 'infinity' ) { + if ( $encodedExpiry != $dbw->getInfinity() ) { $expiry_description = ' (' . wfMsgForContent( 'protect-expiring', $wgContLang->timeanddate( $expiry ), $wgContLang->date( $expiry ) , $wgContLang->time( $expiry ) ) . ')'; } else { @@ -1696,7 +1683,7 @@ class Title { 'pt_namespace' => $namespace, 'pt_title' => $title, 'pt_create_perm' => $create_perm, - 'pt_timestamp' => Block::encodeExpiry( wfTimestampNow(), $dbw ), + 'pt_timestamp' => $dbw->encodeExpiry( wfTimestampNow() ), 'pt_expiry' => $encodedExpiry, 'pt_user' => $wgUser->getId(), 'pt_reason' => $reason, @@ -2037,6 +2024,7 @@ class Title { * contains a array of unique groups. */ public function getCascadeProtectionSources( $getPages = true ) { + global $wgContLang; $pagerestrictions = array(); if ( isset( $this->mCascadeSources ) && $getPages ) { @@ -2082,7 +2070,7 @@ class Title { $purgeExpired = false; foreach ( $res as $row ) { - $expiry = Block::decodeExpiry( $row->pr_expiry ); + $expiry = $wgContLang->formatExpiry( $row->pr_expiry, TS_MW ); if ( $expiry > $now ) { if ( $getPages ) { $page_id = $row->pr_page; @@ -2163,13 +2151,14 @@ class Title { * restrictions from page table (pre 1.10) */ public function loadRestrictionsFromRows( $rows, $oldFashionedRestrictions = null ) { + global $wgContLang; $dbr = wfGetDB( DB_SLAVE ); $restrictionTypes = $this->getRestrictionTypes(); foreach ( $restrictionTypes as $type ) { $this->mRestrictions[$type] = array(); - $this->mRestrictionsExpiry[$type] = Block::decodeExpiry( '' ); + $this->mRestrictionsExpiry[$type] = $wgContLang->formatExpiry( '', TS_MW ); } $this->mCascadeRestriction = false; @@ -2212,7 +2201,7 @@ class Title { // This code should be refactored, now that it's being used more generally, // But I don't really see any harm in leaving it in Block for now -werdna - $expiry = Block::decodeExpiry( $row->pr_expiry ); + $expiry = $wgContLang->formatExpiry( $row->pr_expiry, TS_MW ); // Only apply the restrictions if they haven't expired! if ( !$expiry || $expiry > $now ) { @@ -2241,12 +2230,17 @@ class Title { * restrictions from page table (pre 1.10) */ public function loadRestrictions( $oldFashionedRestrictions = null ) { + global $wgContLang; if ( !$this->mRestrictionsLoaded ) { if ( $this->exists() ) { $dbr = wfGetDB( DB_SLAVE ); - $res = $dbr->select( 'page_restrictions', '*', - array( 'pr_page' => $this->getArticleId() ), __METHOD__ ); + $res = $dbr->select( + 'page_restrictions', + '*', + array( 'pr_page' => $this->getArticleId() ), + __METHOD__ + ); $this->loadRestrictionsFromResultWrapper( $res, $oldFashionedRestrictions ); } else { @@ -2254,7 +2248,7 @@ class Title { if ( $title_protection ) { $now = wfTimestampNow(); - $expiry = Block::decodeExpiry( $title_protection['pt_expiry'] ); + $expiry = $wgContLang->formatExpiry( $title_protection['pt_expiry'], TS_MW ); if ( !$expiry || $expiry > $now ) { // Apply the restrictions @@ -2265,7 +2259,7 @@ class Title { $this->mTitleProtection = false; } } else { - $this->mRestrictionsExpiry['create'] = Block::decodeExpiry( '' ); + $this->mRestrictionsExpiry['create'] = $wgContLang->formatExpiry( '', TS_MW ); } $this->mRestrictionsLoaded = true; } diff --git a/includes/api/ApiQueryBlocks.php b/includes/api/ApiQueryBlocks.php index 584a0498e4..9cea45fb19 100644 --- a/includes/api/ApiQueryBlocks.php +++ b/includes/api/ApiQueryBlocks.php @@ -46,7 +46,7 @@ class ApiQueryBlocks extends ApiQueryBase { } public function execute() { - global $wgUser; + global $wgUser, $wgContLang; $params = $this->extractRequestParams(); if ( isset( $params['users'] ) && isset( $params['ip'] ) ) { @@ -170,7 +170,7 @@ class ApiQueryBlocks extends ApiQueryBase { $block['timestamp'] = wfTimestamp( TS_ISO_8601, $row->ipb_timestamp ); } if ( $fld_expiry ) { - $block['expiry'] = Block::decodeExpiry( $row->ipb_expiry, TS_ISO_8601 ); + $block['expiry'] = $wgContLang->formatExpiry( $row->ipb_expiry, TS_ISO_8601 ); } if ( $fld_reason ) { $block['reason'] = $row->ipb_reason; diff --git a/includes/api/ApiQueryInfo.php b/includes/api/ApiQueryInfo.php index d96808f3f9..5b0d3255a8 100644 --- a/includes/api/ApiQueryInfo.php +++ b/includes/api/ApiQueryInfo.php @@ -397,6 +397,7 @@ class ApiQueryInfo extends ApiQueryBase { * Get information about protections and put it in $protections */ private function getProtectionInfo() { + global $wgContLang; $this->protections = array(); $db = $this->getDB(); @@ -415,7 +416,7 @@ class ApiQueryInfo extends ApiQueryBase { $a = array( 'type' => $row->pr_type, 'level' => $row->pr_level, - 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ) + 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ) ); if ( $row->pr_cascade ) { $a['cascade'] = ''; @@ -472,7 +473,7 @@ class ApiQueryInfo extends ApiQueryBase { $this->protections[$row->pt_namespace][$row->pt_title][] = array( 'type' => 'create', 'level' => $row->pt_create_perm, - 'expiry' => Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 ) + 'expiry' => $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 ) ); } } @@ -506,7 +507,7 @@ class ApiQueryInfo extends ApiQueryBase { $this->protections[$row->tl_namespace][$row->tl_title][] = array( 'type' => $row->pr_type, 'level' => $row->pr_level, - 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ), + 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ), 'source' => $source->getPrefixedText() ); } @@ -529,7 +530,7 @@ class ApiQueryInfo extends ApiQueryBase { $this->protections[NS_FILE][$row->il_to][] = array( 'type' => $row->pr_type, 'level' => $row->pr_level, - 'expiry' => Block::decodeExpiry( $row->pr_expiry, TS_ISO_8601 ), + 'expiry' => $wgContLang->formatExpiry( $row->pr_expiry, TS_ISO_8601 ), 'source' => $source->getPrefixedText() ); } diff --git a/includes/api/ApiQueryProtectedTitles.php b/includes/api/ApiQueryProtectedTitles.php index 32249a7415..6c54dd6771 100644 --- a/includes/api/ApiQueryProtectedTitles.php +++ b/includes/api/ApiQueryProtectedTitles.php @@ -117,7 +117,8 @@ class ApiQueryProtectedTitles extends ApiQueryGeneratorBase { } if ( isset( $prop['expiry'] ) ) { - $vals['expiry'] = Block::decodeExpiry( $row->pt_expiry, TS_ISO_8601 ); + global $wgContLang; + $vals['expiry'] = $wgContLang->formatExpiry( $row->pt_expiry, TS_ISO_8601 ); } if ( isset( $prop['level'] ) ) { diff --git a/includes/db/Database.php b/includes/db/Database.php index d78f4054e9..2a8c1982be 100644 --- a/includes/db/Database.php +++ b/includes/db/Database.php @@ -2747,6 +2747,20 @@ abstract class DatabaseBase implements DatabaseType { return 'infinity'; } + /** + * Encode an expiry time + * + * @param $expiry String: timestamp for expiry, or the 'infinity' string + * @return String + */ + public function encodeExpiry( $expiry ) { + if ( $expiry == '' || $expiry == $this->getInfinity() ) { + return $this->getInfinity(); + } else { + return $this->timestamp( $expiry ); + } + } + /** * Allow or deny "big selects" for this session only. This is done by setting * the sql_big_selects session variable. diff --git a/includes/specials/SpecialProtectedpages.php b/includes/specials/SpecialProtectedpages.php index c676aa001d..5d23788bb5 100644 --- a/includes/specials/SpecialProtectedpages.php +++ b/includes/specials/SpecialProtectedpages.php @@ -80,10 +80,12 @@ class SpecialProtectedpages extends SpecialPage { wfProfileIn( __METHOD__ ); - static $skin=null; + static $skin = null, $dbr = null; - if( is_null( $skin ) ) + if( is_null( $skin ) ){ $skin = $wgUser->getSkin(); + $dbr = wfGetDB( DB_READ ); + } $title = Title::makeTitleSafe( $row->page_namespace, $row->page_title ); $link = $skin->link( $title ); @@ -100,11 +102,15 @@ class SpecialProtectedpages extends SpecialPage { $stxt = ''; - if( $row->pr_expiry != 'infinity' && strlen($row->pr_expiry) ) { - $expiry = Block::decodeExpiry( $row->pr_expiry ); + $expiry = $wgLang->formatExpiry( $row->pr_expiry, TS_MW ); + if( $expiry != $dbr->getInfinity() ) { - $expiry_description = wfMsg( 'protect-expiring' , $wgLang->timeanddate( $expiry ) , - $wgLang->date( $expiry ) , $wgLang->time( $expiry ) ); + $expiry_description = wfMsg( + 'protect-expiring', + $wgLang->timeanddate( $expiry ), + $wgLang->date( $expiry ), + $wgLang->time( $expiry ) + ); $description_items[] = htmlspecialchars($expiry_description); } diff --git a/includes/specials/SpecialProtectedtitles.php b/includes/specials/SpecialProtectedtitles.php index 5b18d87f3b..51528bcc4a 100644 --- a/includes/specials/SpecialProtectedtitles.php +++ b/includes/specials/SpecialProtectedtitles.php @@ -93,7 +93,7 @@ class SpecialProtectedtitles extends SpecialPage { $stxt = ''; if ( $row->pt_expiry != 'infinity' && strlen($row->pt_expiry) ) { - $expiry = Block::decodeExpiry( $row->pt_expiry ); + $expiry = $wgLang->formatExpiry( $row->pt_expiry ); $expiry_description = wfMsg( 'protect-expiring', $wgLang->timeanddate( $expiry ) , $wgLang->date( $expiry ) , $wgLang->time( $expiry ) ); diff --git a/languages/Language.php b/languages/Language.php index 6d971b5d06..504f8826e3 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -2970,6 +2970,37 @@ class Language { return array( $wikiUpperChars, $wikiLowerChars ); } + /** + * Decode an expiry (block, protection, etc) which has come from the DB + * + * @param $expiry String: Database expiry String + * @param $format Bool|Int true to process using language functions, or TS_ constant + * to return the expiry in a given timestamp + * @return String + */ + public function formatExpiry( $expiry, $format = true ) { + static $dbr, $msg; + if( !$dbr ){ + $dbr = wfGetDB( DB_SLAVE ); + $msg = wfMessage( 'infiniteblock' ); + } + + if ( $expiry == '' || $expiry == $dbr->getInfinity() ) { + return $format === true + ? $msg + : $dbr->getInfinity(); + } else { + return $format === true + ? $this->timeanddate( $expiry ) + : wfTimestamp( $format, $expiry ); + } + } + + /** + * @todo Document + * @param $seconds String + * @return string + */ function formatTimePeriod( $seconds ) { if ( round( $seconds * 10 ) < 100 ) { return $this->formatNum( sprintf( "%.1f", round( $seconds * 10 ) / 10 ) ) . $this->getMessageFromDB( 'seconds-abbrev' ); -- 2.20.1